/**
 *
 * @Title: MdzlActivity.java
 * @Package com.idrem.views.bf
 * @Description: TODO(用一句话描述该文件做什么)
 * @author WangZhengCai
 * @date 2012 2012-11-13 下午8:07:59
 * @version V1.0
 *
 */
package com.idrem.views.bf;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;

import com.idrem.R;

/**
 * 
 * @ClassName: MdzlActivity
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author WangZhengCai
 * @date 2012 2012-11-13 下午8:07:59
 * 
 * 
 */
public class MdzlActivity extends Activity {
	private static String TAG = "MdzlActivity";

	private Button btnSignIn = null;
	private ImageView lblImage = null;
	private Button btnCancle = null;

	private static final int PHOTO_PICKED_WITH_DATA = 3021;
	private static final int CAMERA_WITH_DATA = 3023;

	// void showToast(CharSequence msg) {
	// Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	// }

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mdzl);
		btnSignIn = (Button) findViewById(R.id.btn_commit);
		btnSignIn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(MdzlActivity.this,
						SignInActivity.class));

			}
		});
		btnCancle = (Button) findViewById(R.id.btn_cancle);
		btnCancle.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();

			}
		});
		lblImage = (ImageView) findViewById(R.id.iv_btn);
		lblImage.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(intent, CAMERA_WITH_DATA);

			}
		});
		Spinner s1 = (Spinner) findViewById(R.id.spinner_qdlx);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.qdlx, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s1.setAdapter(adapter);
		s1.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// showToast("Spinner1: position=" + position + " id=" + id);
			}

			public void onNothingSelected(AdapterView<?> parent) {
				// showToast("Spinner1: unselected");
			}
		});

		Spinner s2 = (Spinner) findViewById(R.id.spinner_bfjh);
		adapter = ArrayAdapter.createFromResource(this, R.array.bfjh,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s2.setAdapter(adapter);
		s2.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// showToast("Spinner2: position=" + position + " id=" + id);
			}

			public void onNothingSelected(AdapterView<?> parent) {
				// showToast("Spinner2: unselected");
			}
		});
		Spinner s3 = (Spinner) findViewById(R.id.spinner_ywlx);
		adapter = ArrayAdapter.createFromResource(this, R.array.ywlx,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s3.setAdapter(adapter);
		s3.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// showToast("Spinner2: position=" + position + " id=" + id);
			}

			public void onNothingSelected(AdapterView<?> parent) {
				// showToast("Spinner2: unselected");
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {
			String sdStatus = Environment.getExternalStorageState();
			if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
				Log.i("TestFile",
						"SD card is not avaiable/writeable right now.");
				return;
			}
			String name = new DateFormat().format("yyyyMMdd_hhmmss",
					Calendar.getInstance(Locale.CHINA))
					+ ".jpg";
			// Toast.makeText(this, name, Toast.LENGTH_LONG).show();
			Bundle bundle = data.getExtras();
			Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据，并转换为Bitmap图片格式

			FileOutputStream b = null;
			// 保存
			File file = new File("/sdcard/myImage/");
			file.mkdirs();// 创建文件夹
			String fileName = "/sdcard/myImage/" + name;
//			AppLog.e(TAG, "fileName is " + fileName);

			try {
				b = new FileOutputStream(fileName);
				bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
//				AppLog.e(TAG, "fileName saved " + fileName);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} finally {
				try {
					b.flush();
					b.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			((ImageView) findViewById(R.id.iv_btn)).setImageBitmap(bitmap);// 将图片显示在ImageView里
			// Bitmap bitmapRe = this.getPic(fileName + ".jpg");
			// ((ImageView)
			// findViewById(R.id.iv_btn)).setImageBitmap(bitmapRe);//
			// 将图片显示在ImageView里
		}
	}

	public Bitmap getPic(String fileName) {
		// FileInputStream fis = new FileInputStream(fileName);
		// Bitmap bitmap = BitmapFactory.decodeStream(fis);
		Bitmap bitmap = BitmapFactory.decodeStream(getClass()
				.getResourceAsStream(fileName));
		return bitmap;
	}

	protected void doCropPhoto(Bitmap data) {
		Intent intent = getCropImageIntent(data);
		startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
	}

	public static Intent getCropImageIntent(Bitmap data) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setType("image/*");
		intent.putExtra("data", data);
		intent.putExtra("crop", "true");
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		intent.putExtra("outputX", 128);
		intent.putExtra("outputY", 128);
		intent.putExtra("return-data", true);
		return intent;
	}
}
